package com.dididoctor.doctor.WYY;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dididoctor.doctor.WYY.CallEdn.CallEndAttachment;
import com.dididoctor.doctor.WYY.Medicaladvice.MedicaladviceAttachment;
import com.dididoctor.doctor.WYY.Timely.TimelyAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachmentParser;


/**
 * Created by zhoujianghua on 2015/4/9.
 */
public class CustomAttachParser implements MsgAttachmentParser {

    private static final String KEY_TYPE = "type";
    private static final String KEY_DATA = "data";

    @Override
    public MsgAttachment parse(String json) {
        CustomAttachment attachment = null;
        try {
            JSONObject object = JSON.parseObject(json);
            String type = object.getString("messageAttachType");
//            /        private String messageAttachType;
//        private String patientName;
//        private String age;
//        private String illName;
//        private String patientHeadPic;
//        private String recId;
//        private String suggestion;//诊疗建议
//        private String suggestionResult;//诊疗结果
//        private String disId;
//        private String patientPhone;
            String messageAttachType = object.getString("messageAttachType");
            String patientName = object.getString("patientName");
            String age = object.getString("age");
            String illName = object.getString("illName");
            String patientHeadPic = object.getString("patientHeadPic");
            String recId = object.getString("recId");
            String suggestion = object.getString("suggestion");
            String suggestionResult = object.getString("suggestionResult");
            String disId = object.getString("disId");
            String patientPhone = object.getString("patientPhone");
            String sex = object.getString("sex");
            JSONObject data = new JSONObject();

            data.put("messageAttachType", messageAttachType);
            data.put("patientName", patientName);
            data.put("age", age);
            data.put("illName", illName);
            data.put("patientHeadPic", patientHeadPic);
            data.put("recId", recId);
            data.put("suggestion", suggestion);
            data.put("suggestionResult", suggestionResult);
            data.put("disId", disId);
            data.put("patientPhone", patientPhone);
            data.put("sex", sex);
            switch (Integer.parseInt(type)) {
                case CustomAttachmentType.RTS:
                    attachment = new GuessAttachment(CustomAttachmentType.RTS);
                    break;
                case CustomAttachmentType.MEDICCALADVICE:
                    attachment = new MedicaladviceAttachment(CustomAttachmentType.MEDICCALADVICE);
                    break;
                case CustomAttachmentType.APPOINTMENTPHONE:
                    attachment = new CallEndAttachment(CustomAttachmentType.APPOINTMENTPHONE);
                    break;
                case CustomAttachmentType.TIMELYPHONE:
                    attachment =new TimelyAttachment(CustomAttachmentType.TIMELYPHONE);
                    break;
            }
            if (attachment != null) {
                attachment.fromJson(data);
            }

        } catch (Exception e) {

        }

        return attachment;
    }

    public static String packData(int type, JSONObject data) {
        JSONObject object = new JSONObject();
//        object.put(KEY_TYPE, type);
//        if (data != null) {
//            object.put(KEY_DATA, data);
//        }


        object.put("messageAttachType", data.get("messageAttachType"));
        object.put("patientName", data.get("patientName"));
        object.put("age", data.get("age"));
        object.put("illName", data.get("illName"));
        object.put("patientHeadPic", data.get("patientHeadPic"));
        object.put("recId", data.get("recId"));
        object.put("suggestion", data.get("suggestion"));
        object.put("suggestionResult", data.get("suggestionResult"));
        object.put("disId", data.get("disId"));
        object.put("patientPhone", data.get("patientPhone"));
        object.put("sex", data.get("sex"));
        return object.toJSONString();
    }
}
